package com.example.xpvideo.Controller;

import com.example.xpvideo.annotations.IsFile;
import com.example.xpvideo.entity.LsGoodsCategory;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.service.LsGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author hjt
 * @create 2021-08-28 16:15
 * @describe：平台商品分类
 */
@RestController
@RequestMapping("category")
public class GoodsCategoryController {

    @Autowired
    private LsGoodsCategoryService goodsCategoryService;

    /**
     * 查询三级分类
     * @return
     */
    @GetMapping("queryCategory")
    public ResponseResult queryCategory() {
        return goodsCategoryService.queryCategory();
    }

    /**
     * 添加分类
     *
     * @param goodsCategory 分类信息
     * @param file          icon 图片
     * @return
     */
    @PostMapping("insertCategory")
    public ResponseResult insertCategory(@Validated LsGoodsCategory goodsCategory, @Validated @IsFile MultipartFile file, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> list = bindingResult.getAllErrors();
            for(ObjectError  error:list){
                System.out.println(error.getCode()+"---"+error.getArguments()+"---"+error.getDefaultMessage());
            }
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, list);
        }
        return goodsCategoryService.insertCategory(goodsCategory, file);
    }

}
